#include "fePolylineGenerator.h"
#include "feMeshFunction.h"
#include "feMesh.h"
#include "fePolyline.h"
#include <vector>

//---------------------------------------------------------------------------------------------------------------------------------
fePolyline<float> fePolylineGenerator::CreatePolylineFuncLayer(feMeshFunction *iMeshFunc, int iLayerNum)
{
	fePolyline<float> line;
	line.Allocate(iMeshFunc->GetMesh()->NodesCount());
	
	//extract layer
	for(int i=0; i<iMeshFunc->GetMesh()->NodesCount(); i++)
	{
		line.Push(iMeshFunc->GetMesh()->GetNode(i).X, iMeshFunc->GetU(i, iLayerNum));
	}


	//create line
	return line;
}


//----------------------------------------------------------------------------------------------------------------------------------
fePolyline<float> fePolylineGenerator::CreatePolylineFromArray(double *iX, double *iF, int iCount, double iStepX)
{
	fePolyline<float> line;
	
	line.Allocate(iCount);

	for(int i=0; i<iCount; i++)
		line.Push(float(iX[i]), iF[i]);

	//done
	return line;
}



//-----------------------------------------------------------------------------------------------------------------------------------
bool fePolylineGenerator::SavePolylineToFile(fePolyline<double> *iPolyline, char *iFileName)
{
	/*//open
	FILE *fp = fopen(iFileName, "wb");
	if(!fp) return false;


	//write n
	int n = iPolyline->GetSize();
	fwrite(&n, sizeof(int), 1, fp);


	//write x z
	fwrite(iPolyline->ToArray(), sizeof(fePolyline<float>::V2), iPolyline->GetSize(), fp);


	//done
	fclose(fp);*/

	//open
	FILE *fp = fopen(iFileName, "w");
	if(!fp) return false;


	//write head
	fprintf(fp, "\n\tt (mc)         E (mB)\n");
	

	//write line
	int n = iPolyline->GetSize();
	for(int i=0; i<n; i++)
	{
		fprintf(fp, "%e %e\n", iPolyline->GetPoint(i).X, iPolyline->GetPoint(i).Y);
	}

	//done
	fclose(fp);

	return true;
}

//-----------------------------------------------------------------------------------------------------------------------------------
bool fePolylineGenerator::SavePolylineToFile(fePolyline<float> *iPolyline, char *iFileName)
{
	/*//open
	FILE *fp = fopen(iFileName, "wb");
	if(!fp) return false;


	//write n
	int n = iPolyline->GetSize();
	fwrite(&n, sizeof(int), 1, fp);


	//write x z
	fwrite(iPolyline->ToArray(), sizeof(fePolyline<float>::V2), iPolyline->GetSize(), fp);


	//done
	fclose(fp);*/

	//open
	FILE *fp = fopen(iFileName, "w");
	if(!fp) return false;


	//write head
	fprintf(fp, "\n\tt (mc)         E (mB)\n");
	

	//write line
	int n = iPolyline->GetSize();
	for(int i=0; i<n; i++)
	{
		fprintf(fp, "%e %e\n", iPolyline->GetPoint(i).X, iPolyline->GetPoint(i).Y);
	}

	//done
	fclose(fp);

	return true;
}